package com.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;

/**
 * @Description TODO(这里用一句话描述这个类的作用)
 *
 * @author liuqinghua
 * @date 2024-7-18
 */
public class DocTemplateUtil {

    public static ByteArrayOutputStream buildDocOutputStream(DocTmplateDataDto dto, InputStream templateStream)
            throws IOException {
        // 绑定插件
        ConfigureBuilder configBuilder = Configure.builder();
        for (String key : dto.getListNames()) {
            configBuilder.bind(key, new LoopRowTableRenderPolicy());
        }
        Configure config = configBuilder.build();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        XWPFTemplate template = XWPFTemplate.compile(templateStream, config).render(dto.getData());
        // 写入到byteArrayOutputStream
        template.write(byteArrayOutputStream);
        return byteArrayOutputStream;
    }

}
